layui.use(['form', 'upload', 'fsoft'], function () {
  var $ = layui.$
    , form = layui.form
    , upload = layui.upload
    , fsoft = layui.fsoft;
  var id = $('#id').val();
  var roleIds = $('#currUserRoleId').val();
  var mainAction = {
    render: function () {
      mainAction.renderRoleSelect();
      mainAction.renderOrgSelect();
      form.render();
    }
    , renderRoleSelect: function () {//查询当前登录用户已有的角色列表,加载到角色的下拉列表中
      $.post(layui.cache['contentPath'] + "/sys-role/findList", {}, function (data) {
        data = eval('(' + data + ')');
        if (data.code == "0") {
          xmSelect.render({
            el: '#roleIdList'
            , layVerify: 'required'
            , name: "roleIdList",
            initValue: roleIds.split(","),
            prop: { name: 'name', value: 'id' }, theme: { color: '#0081ff' },
            data: data.data, paging: true, pageSize: 5
          });
        }
      });
    }
    , renderOrgSelect: function () {//组织机构列表
      $.post(layui.cache['contentPath'] + "/sys-org/findList", { parents: fsoft.getUser().orgId }, function (data) {
        data = eval('(' + data + ')');
        if (data.code == "0") {
          xmSelect.render({
            el: '#xm-select-orgTrees',
            radio: true,
            layVerify: 'required',
            initValue: [$('#orgId').val()],
            prop: { name: 'name', value: 'id' },
            theme: { color: '#0081ff' },
            data: data.data,
            filterable: true,
            clickClose: true, paging: true, pageSize: 5,
            name: "orgId",
            on: function (data) {
              var arr = data.arr;
              if (data.isAdd) {
                $('#orgId').val(arr[0].id);
              }
              mainAction.renderDeptSelect();
            }
          });
          mainAction.renderDeptSelect();
        }
      });
    }
    , renderDeptSelect: function (orgId) {//所属部门
      if (orgId == undefined || orgId == null)
        orgId = $('#orgId').val();
      $.post(layui.cache['contentPath'] + "/sys-dept/findList", { status: 1, orgId }, function (data) {
        data = eval('(' + data + ')');
        if (data.code == "0") {
          xmSelect.render({
            el: '#selectDept'
            , radio: true
            , layVerify: 'required'
            , name: "deptId",
            initValue: [$('#deptId').val()],
            prop: { name: 'name', value: 'id' }, theme: { color: '#0081ff' },
            data: data.data, filterable: true, clickClose: true, paging: true, pageSize: 5
          });
        }
      });
    }
  };
  //界面初始化
  mainAction.render();

  /* 头像上传控件  */
  upload.render({
    elem: '#imagePath'
    , url: layui.cache['contentPath'] + '/sms-common/upload'
    , size: 4096
    , done: function (res, index) {
      if (res.code == 0) {
        var img = res.data[0];
        $('#imagePath').attr('src', layui.cache['contentPath'] + "/" + img.relativePath);
        $("#imgPath").val(img.relativePath);
      }
    }
  });

  // 监听提交
  form.on('submit(save)', function (data) {
    var submitUrl = layui.cache['contentPath'] + '/sys-user/save';
    if (id != undefined && id != "") {
      submitUrl = layui.cache['contentPath'] + '/sys-user/modify';
    }
    fsoft.put(submitUrl, data.field, function (rs) {
      if (rs.code == 0) {
        fsoft.successMsg("操作成功");
        xadmin.close();
        xadmin.father_reload();
      } else {
        fsoft.errorMsg(rs.msg);
      }
    });
    return false;
  });

  form.on('radio(isGongys)', function (data) {
    $("#isGongys").val(data.value);
    mainAction.render();
  });
  /**
   * 自定义校验规则
   */
  form.verify({
    //登录帐号合法性校验
    confirmLoginAcct: function (value, item) {
      if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
        return '用户名不能有特殊字符';
      }
      if (/(^\_)|(\__)|(\_+$)/.test(value)) {
        return '用户名首尾不能出现下划线\'_\'';
      }
    }
  });
});